package com.net.xpay.common.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.model.SignAuthModel;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.common.exception.BusinessException;
import com.net.common.util.AlipayAccountCheckUtil;
import com.net.common.util.EmailUtil;
import com.net.common.util.SerializationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 用户签约参数验证
 */
@Slf4j
@Component
public class AlipayUserSignValidator {

    /**
     * 签约授权一体化
     */
    public void validateSignAuth(User user, SignAuthModel signAuthModel) {
        log.info("validateSignAuth params={}|userId={}", SerializationUtil.obj2String(signAuthModel), user.getId());
        if (Strings.isNullOrEmpty(signAuthModel.getAlipayNickName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入支付宝账号");
        }

        if (!AlipayAccountCheckUtil.isEffectiveAccount(signAuthModel.getAlipayNickName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号格式错误");
        }
        if (Strings.isNullOrEmpty(signAuthModel.getContactMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入联系人手机号");
        }
        if (Strings.isNullOrEmpty(signAuthModel.getContactName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入联系人真实姓名");
        }
        if (!Strings.isNullOrEmpty(signAuthModel.getContactEmail())) {
            if (!EmailUtil.checkEmail(signAuthModel.getContactEmail())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "联系人邮箱格式不正确");
            }
        }

    }
}
